﻿using LiteDB;
using NovelFetch.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NovelFetch
{
    public partial class FilterForm : Form
    {
        private List<TextFilterRule> _filterRule;
        private EditFlag _editFlag = EditFlag.View;
        public FilterForm()
        {
            InitializeComponent();
            _filterRule = new List<TextFilterRule>();
            InitData();
            
        }

        private void InitData()
        {
            using (var db = new LiteDatabase(@"novel.db"))
            {
                var liteCollection = db.GetCollection<TextFilterRule>();
                liteCollection.EnsureIndex(w => w.OrgText);
                if (liteCollection.Count() > 0)
                {
                    _filterRule = liteCollection.FindAll().ToList();
                    foreach(var r in _filterRule)
                    {
                        var strBool = r.IsSupportRegex ? "是" : "否";
                        lvTextFilter.Items.Add(new ListViewItem(new string[] { r.OrgText, r.ReplaceText, strBool }));
                    }
                }

            }
        }

        private void lvTextFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                _editFlag = EditFlag.View;
                if (lvTextFilter.SelectedIndices.Count > 0)
                {
                    var seletIdx = lvTextFilter.SelectedIndices[0];
                    var selectItem = lvTextFilter.Items[seletIdx];
                    tbOrgText.Text = selectItem.SubItems[0].Text;
                    tbReplaceText.Text = selectItem.SubItems[1].Text;
                    cbSupportRegex.Checked = "是".Equals(selectItem.SubItems[2].Text, StringComparison.CurrentCulture) ? true : false;
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("选择错误:" + ex.Message);
            }
            
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var orgText = tbOrgText.Text;
            if (string.IsNullOrEmpty(orgText))
            {
                MessageBox.Show("原文不能为空");
                return;
            }
            _editFlag = EditFlag.Add;
            if (SaveChange())
            {
                var replaceText = tbReplaceText.Text??"";
                var strSupportRegex = cbSupportRegex.Checked ? "是" : "否";
                lvTextFilter.Items.Add(new ListViewItem(new string[] { orgText, replaceText, strSupportRegex }));

                tbOrgText.Clear();
                tbReplaceText.Clear();
                cbSupportRegex.Checked = false;
            }


        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            var orgText = tbOrgText.Text;
            if (string.IsNullOrEmpty(orgText))
            {
                MessageBox.Show("请先选择需要修改的项");
                return;
            }
            _editFlag = EditFlag.Modify;
            if (SaveChange())
            {
                var replaceText = tbReplaceText.Text??"";
                var strSupportRegex = cbSupportRegex.Checked ? "是" : "否";
                var seletIdx = lvTextFilter.SelectedIndices[0];
                lvTextFilter.Items[seletIdx].SubItems[0].Text = orgText;
                lvTextFilter.Items[seletIdx].SubItems[1].Text = replaceText;
                lvTextFilter.Items[seletIdx].SubItems[2].Text = strSupportRegex;
            }
            
            
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            var orgText = tbOrgText.Text;
            if (string.IsNullOrEmpty(orgText))
            {
                MessageBox.Show("请先选择需要删除的项");
                return;
            }
            _editFlag = EditFlag.Del;
            if (SaveChange())
            {
                var seletIdx = lvTextFilter.SelectedIndices[0];
                lvTextFilter.Items.RemoveAt(seletIdx);
                tbOrgText.Clear();
                tbReplaceText.Clear();
                cbSupportRegex.Checked = false;
            }
            
            
        }

        private bool SaveChange()
        {
            bool bRet = true;
            try
            {
                using (var db = new LiteDatabase(@"novel.db"))
                {
                    var liteCollection = db.GetCollection<TextFilterRule>();
                    var orgText = tbOrgText.Text;
                    var replaceText = tbReplaceText.Text;
                    var bSupportRegex = cbSupportRegex.Checked;
                    var item = liteCollection.FindOne(w => w.OrgText == orgText);
                    switch (_editFlag)
                    {
                        case EditFlag.Add:
                            if(item != null)
                            {
                                MessageBox.Show("过滤规则已存在","规则错误");
                                return false;
                            }
                            var model = new TextFilterRule { OrgText = orgText, ReplaceText = replaceText, IsSupportRegex = bSupportRegex };
                            liteCollection.Insert(model);
                            
                            break;
                        case EditFlag.Modify:
                            if (item != null)
                            {
                                item.OrgText = orgText;
                                item.ReplaceText = replaceText;
                                item.IsSupportRegex = bSupportRegex;
                                bRet = liteCollection.Update(item);
                            }
                            else
                            {
                                var model1 = new TextFilterRule { OrgText = orgText, ReplaceText = replaceText, IsSupportRegex = bSupportRegex };
                                liteCollection.Insert(model1);
                            }
                            break;
                        case EditFlag.Del:
                            bRet = liteCollection.Delete(w => w.OrgText == orgText) > 0;
                            
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"错误:{ex.Message}");
                bRet = false;
            }
            return bRet;
            
        }
    }
}
